package com.gxa.uhome.controller;

import com.gxa.uhome.pojo.Admin;
import com.gxa.uhome.pojo.Menu;
import com.gxa.uhome.pojo.Role;
import com.gxa.uhome.service.AdminService;
import com.gxa.uhome.service.LogService;
import com.gxa.uhome.service.impl.AdminServiceImpl;
import com.gxa.uhome.util.JsonResult;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: qingyao
 * @date: 12:45 2021/6/5
 */
@RestController
@RequestMapping("/admin")
@Api(value = "管理员管理模块", produces = "application/json;charset=UTF-8", tags = "Admin管理员管理模块")
@CrossOrigin
public class AdminController {
    @Resource
    HttpSession httpSession;
    @Resource
    AdminService adminService = new AdminServiceImpl();
    @Resource
    LogService logService = null;

    //根据adminid得到roleid再得到rolename
    @ApiOperation(value = "啥也没有",
            notes = "请查看",//response = Map.class,
            httpMethod = "POST")
    @PostMapping("/getall")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页数"),
            @ApiImplicitParam(name = "size", value = "大小长度")
    })
//    @ResponseBody
    public JsonResult getall(@RequestParam(value = "page", defaultValue = "1", required = false) Integer page, @RequestParam(value = "size", defaultValue = "5", required = false) Integer size) throws Exception {
        JsonResult jsonResult = new JsonResult();
        List<Admin> getrolename = adminService.getall((page - 1) * size, size);
        if (getrolename != null) {
            jsonResult.setCode("1");
            jsonResult.setMsg("查询成功");
            jsonResult.setData(getrolename);
        } else {
            jsonResult.setCode("0");
            jsonResult.setMsg("查询失败");
        }
        System.out.println(getrolename);
        return jsonResult;
    }

    @ApiOperation(value = "输入用户名(nickName)密码进行登录",
            notes = "请查看",//response = Map.class,
            httpMethod = "POST")
    @PostMapping("/login")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "nickName", value = "用户名"),
            @ApiImplicitParam(name = "pwd", value = "密码")
    })
    @ResponseBody
    public JsonResult login(@RequestParam(value = "nickName", required = true) String nickName,
                            @RequestParam(value = "pwd", required = true) String pwd) throws Exception {
        JsonResult jsonResult = new JsonResult();
        Admin admin = new Admin();
        admin.setNickName(nickName);
        admin.setPwd(pwd);
        Admin login = adminService.login(admin);
        if (login != null) {
            jsonResult.setCode("1");
            jsonResult.setMsg("登录成功");
            jsonResult.setData(login);
            String token = "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJmcm9tX3VzZXIiOiJCIiwidGFyZ2V0X3VzZXIiOiJBIn0.rSWamyAYwuHCo7IFAgd1oRpSP7nzL7BF5t7ItqpKViM";
            jsonResult.setValue("token", token);
/*            httpSession.setAttribute("AdminId", login.getId());
            httpSession.setAttribute("AdminNickName", login.getNickName());*/
            logService.insert("后台用户登录", login.getId(), "登录账号" + login.getNickName());
        } else {
            jsonResult.setCode("0");
            jsonResult.setMsg("登录失败");
        }
        System.out.println("用户名" + nickName);
        System.out.println("密码" + pwd);
        System.out.println(login);
        return jsonResult;
    }

/*    @ApiOperation(value = "退出当前账户(清除HttpSession)",
            notes = "退出",//response = Map.class,
            httpMethod = "POST")
    @PostMapping("/signout")
    public JsonResult signout(HttpSession httpSession) throws Exception {
        JsonResult jsonResult = new JsonResult();
        httpSession.removeAttribute("AdminId");
        httpSession.removeAttribute("AdminNickName");
        if (httpSession.getAttribute("AdminId") == null && httpSession.getAttribute("AdminNickName") == null) {
            jsonResult.setCode("1");
            jsonResult.setMsg("退出成功");
            jsonResult.setData("登出成功");
        } else {
            jsonResult.setCode("0");
            jsonResult.setMsg("退出失败");
            jsonResult.setData("出现异常");
        }
        return jsonResult;
    }*/

    @ApiOperation(value = "搜索角色名或模糊名字得到admin的操作",
            notes = "下拉框选择角色进行查询",//response = Admin.class,
            httpMethod = "POST")
    @PostMapping("/select")
    @ApiImplicitParam(name = "role", value = "角色")
    @ResponseBody
    public JsonResult selectrole(@RequestParam(value = "role", required = false) String role, @RequestParam(value = "name", required = false) String name, @RequestParam(value = "page", defaultValue = "1", required = false) Integer page, @RequestParam(value = "size", defaultValue = "5", required = false) Integer size) throws Exception {
        JsonResult jsonResult = new JsonResult();
        List<Admin> select = adminService.select(role, name, page, size);
        if (select != null) {
            jsonResult.setCode("1");
            jsonResult.setMsg("查询成功");
            jsonResult.setData(select);
        } else {
            jsonResult.setCode("0");
            jsonResult.setMsg("查询失败");
        }
        return jsonResult;
    }

/*    @ApiOperation(value = "根据模糊查询得到admin的操作",
            notes = "输入真实姓名进行查询",//response = Admin.class,
            httpMethod = "post")
    @PostMapping("/selectlike")
            @ApiImplicitParam(name = "realname",value = "真实姓名")
    @ResponseBody
    public JsonResult selectlike(@RequestParam(value = "realname",required = false) String realname) throws Exception{
        JsonResult jsonResult=new JsonResult();
        List<Admin> select = adminMapper.selectlike("%"+realname+"%");
        if(select!=null){
            jsonResult.setCode("1");
            jsonResult.setMsg("查询成功");
            jsonResult.setData(select);
        }else{
            jsonResult.setCode("0");
            jsonResult.setMsg("查询失败");
        }
        return jsonResult;
    }*/

    @ApiOperation(value = "新增操作员的操作",
            notes = "请查看",//response = Admin.class,
            httpMethod = "POST")
    //httpmethod请求方式
    @PostMapping("/add")
    @ApiImplicitParam(name = "admin", value = "管理员类")
    @ResponseBody
    public JsonResult add(@RequestBody Admin admin) throws Exception {
        JsonResult jsonResult = new JsonResult();
        Integer insert = adminService.insert(admin);
        if (insert >= 1) {
            jsonResult.setCode("1");
            jsonResult.setMsg("添加成功");
            jsonResult.setData(insert);
            logService.insert("后台用户添加", (Integer) httpSession.getAttribute("AdminId"), "登录账号" + httpSession.getAttribute("AdminNickName"));
        } else {
            jsonResult.setCode("0");
            jsonResult.setMsg("添加失败");
        }
        return jsonResult;
    }

    @ApiOperation(value = "编辑操作员的操作",
            notes = "请查看",//response = Admin.class,
            httpMethod = "POST")
    //httpmethod请求方式
    @PostMapping("/update")
    @ApiImplicitParam(name = "admin", value = "管理员类")
    @ResponseBody
    public JsonResult update(@RequestBody Admin admin) throws Exception {
        JsonResult jsonResult = new JsonResult();
        Integer update = adminService.update(admin);
        if (update >= 1) {
            jsonResult.setCode("1");
            jsonResult.setMsg("修改成功");
            jsonResult.setData(update);
            logService.insert("后台用户修改", (Integer) httpSession.getAttribute("AdminId"), "登录账号" + httpSession.getAttribute("AdminNickName"));
        } else {
            jsonResult.setCode("0");
            jsonResult.setMsg("修改失败");
        }
        return jsonResult;
    }

    @ApiOperation(value = "根据角色id删除该角色的操作",
            notes = "请查看",//response = Admin.class,
            httpMethod = "POST")
    //httpmethod请求方式
    @PostMapping("/delete")
    @ApiImplicitParam(name = "id", value = "Adminid 管理员id",
            example = "1")
    @ResponseBody
    public JsonResult deletebyid(@RequestParam Integer[] id) throws Exception {
        JsonResult jsonResult = new JsonResult();
        Integer delete = adminService.delete(id);
        if (delete >= 1) {
            jsonResult.setCode("1");
            jsonResult.setMsg("删除成功");
            jsonResult.setData(delete);
        } else {
            jsonResult.setCode("0");
            jsonResult.setMsg("删除失败");
        }
        return jsonResult;
    }

    @ApiOperation(value = "根据管理员id重置该角色密码的操作",
            notes = "重置密码123456",//response = Admin.class,
            httpMethod = "POST")
    @PostMapping("/resetpwd")
    @ApiImplicitParam(name = "id", value = "Adminid 管理员id")
    @ResponseBody
    public JsonResult resetpwd(@RequestParam Integer id) throws Exception {
        JsonResult jsonResult = new JsonResult();
        Integer resetpwd = adminService.resetpwd(id);
        if (resetpwd >= 1) {
            jsonResult.setCode("1");
            jsonResult.setMsg("修改成功");
            jsonResult.setData(resetpwd);
        } else {
            jsonResult.setCode("0");
            jsonResult.setMsg("修改失败");
        }
        return jsonResult;
    }

    @ApiOperation(value = "根据管理员id修改管理员密码的操作",
            notes = "修改密码",//response = Admin.class,
            httpMethod = "POST")
    //httpmethod请求方式
    @PostMapping("/changepwd")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "Adminid 管理员id"),
            @ApiImplicitParam(name = "pwd", value = "更改后的密码")
    })
    @ResponseBody
    public JsonResult changepwd(@RequestParam Integer id, @RequestParam String pwd) throws Exception {
        JsonResult jsonResult = new JsonResult();
        Admin admin = new Admin();
        admin.setId(id);
        admin.setPwd(pwd);
        Integer changepwd = adminService.changepwd(admin);
        if (changepwd >= 1) {
            jsonResult.setCode("1");
            jsonResult.setMsg("修改成功");
            jsonResult.setData(changepwd);
        } else {
            jsonResult.setCode("0");
            jsonResult.setMsg("修改失败");
        }
        return jsonResult;
    }

    @GetMapping("/menu")
    public JsonResult selectAdminAndMenuById(@RequestParam Integer id) {
        JsonResult jsonResult = new JsonResult();
        Admin admin = adminService.selectAdminAndMenuById(id);
        List<Menu> pid = new ArrayList<>();
        List<Menu> all = new ArrayList<>();

        for (Role role : admin.getRoleList()) {
            for (Menu menu : role.getMenuList()) {
                all.add(menu);
                if (menu.getPid() == 0) {
                    pid.add(menu);
                }
            }
        }
        for (Menu menu1 : pid) {
            List<Menu> zi = new ArrayList<>();
            for (Menu menu2 : all) {
                if (menu2.getPid().equals(menu1.getId())) {
                    zi.add(menu2);
                }
            }
            menu1.setMenuChildren(zi);
        }
        if (admin != null) {
            jsonResult.setCode("1");
            jsonResult.setMsg("菜单查询成功");
            jsonResult.setData(pid);
        } else {
            jsonResult.setCode("0");
            jsonResult.setMsg("菜单查询失败");
        }
        return jsonResult;
    }

}
